﻿--[[
    @brief  GameList sync view
    @author jameszhai
    @date   2017-03-23
]]

ETipsResUI = 
{
    uiLayout = "GameSyncTips.tpf";

    uiTitle  = "pnlClient\\lblTitle";
    uiTips   = "pnlClient\\edtTips";
    uiCfmBtn = "pnlClient\\btnConfirm";
    uiCelBtn = "pnlClient\\btnCancel";
    uiClsBtn = "TitleBarCtn\\btnClose";

    sDefBtnBG = "ONEIMAGE:client_tips\\btn2_00.png;Stretch;null;";
    sDefHovBG = "ONEIMAGE:client_tips\\btn2_01.png;Stretch;null;";
    sDefClkBG = "ONEIMAGE:client_tips\\btn2_02.png;Stretch;null;";

    sNrlBtnBG = "ONEIMAGE:client_tips\\btn_00.png;Stretch;null;";
    sNrlHovBG = "ONEIMAGE:client_tips\\btn_01.png;Stretch;null;";
    sNrlClkBG = "ONEIMAGE:client_tips\\btn_02.png;Stretch;null;";

};

ESyncTipsType = 
{
    sttClsTips = 0;     -- 关闭后提示
    sttClsCfm  = 1;     -- 关闭确认
    sttFstTips = 2;     -- 首次运行提示
};

EClickType = 
{
    ctClose   = 1;       -- 关闭
    ctEnable  = 2;       -- 开启
    ctDisable = 3;       -- 不开启
};

TGMListSyncView = 
{
    InitView = function()
    end;

    UnInitView = function()
    end;

    -- 注意：后续请尽量使用通用tips组件 ui_utility.show_tips(param);
    -- 显示TIPS
    ShowTips = function(nType)
        if (ESyncTipsType.sttClsTips == nType) then
            TGMListSyncView.ShowView(LoadStr(ID_CLOUD_LIST_TIPS1),
                LoadStr(ID_CLOUD_LIST_TIPS2),
                nil, LoadStr(ID_CLOUD_LIST_TIPS3), 1);
        elseif (ESyncTipsType.sttClsCfm == nType) then
            TGMListSyncView.ShowView(LoadStr(ID_CLOUD_LIST_TIPS4),
                LoadStr(ID_CLOUD_LIST_TIPS5),
                LoadStr(ID_CLOUD_LIST_TIPS6), LoadStr(ID_CLOUD_LIST_TIPS7), 1);
        elseif (ESyncTipsType.sttFstTips == nType) then
            TGMListSyncView.ShowView(LoadStr(ID_CLOUD_LIST_TIPS8),
                LoadStr(ID_CLOUD_LIST_TIPS9),
                LoadStr(ID_CLOUD_LIST_TIPS10), LoadStr(ID_CLOUD_LIST_TIPS11), 0);
            -- 上报首次运行弹窗
            TGMListSyncView.ReportFstRun();
        else
            TGPLog.LogE("GameListSync", "Unknown type:" .. tostring(nType));
            return;
        end;

        TGMListSyncView.eTipType = nType;
    end;

    -- 显示界面
    ShowView = function(sTitle, sTips, sConfirm, sCancel, nDefault)
        if not TGMListSyncView.CreateUI() then
            TGPLog.LogE("GameListSync", "CreateUI fail.");
            return;
        end;

        local   pDialog = ui_utility.get_ctrl_ptr_by_id(
                            TGMListSyncView.dwViewID);

        if (nil == pDialog) then
            TGPLog.LogE("GameListSync", "Dialog is null.");
            return;
        end;

        local   rtTemp = pDialog:GetRectEx(FALSE,TRUE);

        -- 标题栏
        local   pTitle = ui_utility.get_ctrl_ptr_by_path(
            TGMListSyncView.dwViewID, ETipsResUI.uiTitle);
        if nil ~= pTitle then
            local   sText = U8toCUIStr(sTitle);
            pTitle:SetText(sText);
        else
            TGPLog.LogE("GameListSync", "Title label is null.");
        end;

        -- 提示文字
        local   pTips = ui_utility.get_ctrl_ptr_by_path(
            TGMListSyncView.dwViewID, ETipsResUI.uiTips);
        if (nil ~= pTips) then
            local   sText = U8toCUIStr(sTips);
            pTips:SetText(sText);
        else
            TGPLog.LogE("GameListSync", "Tips label is null.");
        end;

        local   pBtnCfm = ui_utility.get_ctrl_ptr_by_path(
            TGMListSyncView.dwViewID, ETipsResUI.uiCfmBtn);
        if nil ~= pBtnCfm then
            if (nil ~= sConfirm) then
                local   sText = U8toCUIStr(sConfirm);
                pBtnCfm:SetCaption(sText);
                pBtnCfm:Show(TRUE);
            else
                pBtnCfm:Show(FALSE);
            end;
        else
            TGPLog.LogE("GameListSync", "Config button is null.");
        end;

        local   pBtnCel = ui_utility.get_ctrl_ptr_by_path(
            TGMListSyncView.dwViewID, ETipsResUI.uiCelBtn);

        if nil ~= pBtnCel then
            if (nil ~= sCancel) then
                local   sText = U8toCUIStr(sCancel);
                pBtnCel:SetCaption(sText);
                pBtnCel:Show(TRUE);
            else
                pBtnCel:Show(FALSE);
            end;
        else
            TGPLog.LogE("GameListSync", "Cancel button is null.");
        end;

        if (nil ~= pBtnCfm) and (nil ~= pBtnCel) then
            if 0 == nDefault then
                
                ui_utility.setproperty_by_id(pTitle:GetControlID(),
                    "YOffset", 27);
                ui_utility.setproperty_by_id(pTips:GetControlID(),
                    "YOffset", 53);

                ui_utility.setproperty_by_id(pBtnCfm:GetControlID(),
                    "Background", ETipsResUI.sDefBtnBG);
                ui_utility.setproperty_by_id(pBtnCfm:GetControlID(),
                    "BackgroundHover", ETipsResUI.sDefHovBG);
                ui_utility.setproperty_by_id(pBtnCfm:GetControlID(),
                    "BackgroundPressed", ETipsResUI.sDefClkBG);

                ui_utility.setproperty_by_id(pBtnCel:GetControlID(),
                    "Background", ETipsResUI.sNrlBtnBG);
                ui_utility.setproperty_by_id(pBtnCel:GetControlID(),
                    "BackgroundHover", ETipsResUI.sNrlHovBG);
                ui_utility.setproperty_by_id(pBtnCel:GetControlID(),
                    "BackgroundPressed", ETipsResUI.sNrlClkBG);
            else
                ui_utility.setproperty_by_id(pBtnCfm:GetControlID(),
                    "Background", ETipsResUI.sNrlBtnBG);
                ui_utility.setproperty_by_id(pBtnCfm:GetControlID(),
                    "BackgroundHover", ETipsResUI.sNrlHovBG);
                ui_utility.setproperty_by_id(pBtnCfm:GetControlID(),
                    "BackgroundPressed", ETipsResUI.sNrlClkBG);

                ui_utility.setproperty_by_id(pBtnCel:GetControlID(),
                    "Background", ETipsResUI.sNrlBtnBG);
                ui_utility.setproperty_by_id(pBtnCel:GetControlID(),
                    "BackgroundHover", ETipsResUI.sNrlHovBG);
                ui_utility.setproperty_by_id(pBtnCel:GetControlID(),
                    "BackgroundPressed", ETipsResUI.sNrlClkBG);
            end;

            pDialog:MoveToCenter();
            pDialog:Show(TRUE);
        else
            TGPLog.LogE("GameListSync", "Control is null.");
        end;
    end;

    -- 隐藏界面
    HideView = function()
        local   pDialog = ui_utility.get_ctrl_ptr_by_id(
                    TGMListSyncView.dwViewID);

        if nil ~= pDialog then
            pDialog:Show(FALSE);
        else
            TGPLog.LogE("GameListSync", "Dialog is null.");
        end;
    end;

    -- 创建UI
    CreateUI = function()
        if TGMListSyncView.IsValidID(TGMListSyncView.dwViewID) then
            TGPLog.LogI("GameSyncView", "UI already created.");
            return true;
        end;

        TGMListSyncView.dwViewID = layout_mgr.restore_layout_multi(ETipsResUI.uiLayout);

        if TGMListSyncView.IsValidID(TGMListSyncView.dwViewID) then
            local   dwCtrlID = ui_utility.get_ctrl_id_by_path(TGMListSyncView.dwViewID,
                                    ETipsResUI.uiCfmBtn);

            if (TGMListSyncView.IsValidID(dwCtrlID)) then
                ui_utility.setproperty_by_id(dwCtrlID, "OnClick", 
                    "TGMListSyncView.OnClkConfirm");
            else
                TGPLog.LogE("GameSyncView", "Get confirm button fail.");
                return false;
            end;

            local   dwCtrlID = ui_utility.get_ctrl_id_by_path(TGMListSyncView.dwViewID,
                                    ETipsResUI.uiCelBtn);

            if (TGMListSyncView.IsValidID(dwCtrlID)) then
                ui_utility.setproperty_by_id(dwCtrlID, "OnClick", 
                    "TGMListSyncView.OnClkCancel");
            else
                TGPLog.LogE("GameSyncView", "Get cancel button fail.");
                return false;
            end;

            local   dwCtrlID = ui_utility.get_ctrl_id_by_path(TGMListSyncView.dwViewID,
                                    ETipsResUI.uiClsBtn);

            if (TGMListSyncView.IsValidID(dwCtrlID)) then
                ui_utility.setproperty_by_id(dwCtrlID, "OnClick", 
                    "TGMListSyncView.OnClkClose");
            else
                TGPLog.LogE("GameSyncView", "Get close button fail.");
                return false;
            end;

            return true;
        else
            TGPLog.LogE("GameSyncView", "Restore layout fail.");
            return false;
        end;
    end;

    -- 点击关闭按钮
    OnClkClose = function()
        TGMListSyncView.HideView();

        -- 点击关闭按钮
        if TGMListSyncView.eTipType == ESyncTipsType.sttFstTips then
            TGMListSyncView.EnableCloud(1);
        end;

        TGMListSyncView.ReportClick(EClickType.ctClose);
    end;

    -- 点击取消按钮
    OnClkCancel = function()
        TGMListSyncView.HideView();

        if TGMListSyncView.eTipType == ESyncTipsType.sttFstTips then
            TGMListSyncView.EnableCloud(0);
            TGMListSyncView.ReportClick(EClickType.ctDisable);
        else
            TGMListSyncView.ReportClick(EClickType.ctClose);
        end;
    end;

    -- 点击回调
    SetClkSink = function(sSink)
        TGMListSyncView.sClkSink = sSink;
    end;

    -- 是否开启
    EnableCloud = function(nEnable)
        TGMListSyncView.nEnabled = nEnable;

        if nil ~= TGMListSyncView.sClkSink then
            local   sCode = TGMListSyncView.sClkSink .. "(TGMListSyncView.nEnabled);"
            local   pCode = loadstring(sCode);

            if nil ~= pCode then
                pCode();
            end;
        end;
    end;

    -- 点击确定按钮
    OnClkConfirm = function()
        TGMListSyncView.HideView();

        if TGMListSyncView.eTipType == ESyncTipsType.sttClsTips then
            TGPLog.LogE("GameListSync", "Error branch.");
        elseif TGMListSyncView.eTipType == ESyncTipsType.sttClsCfm then
            TGMListSyncView.EnableCloud(0);     -- 关闭
            TGMListSyncView.ReportClick(EClickType.ctDisable);
        elseif TGMListSyncView.eTipType == ESyncTipsType.sttFstTips then
            TGMListSyncView.EnableCloud(1);     -- 开启
            TGMListSyncView.ReportClick(EClickType.ctEnable);
        else
            TGPLog.LogE("GameListSync", "Unknown type:"..
                        tostring(TGMListSyncView.eTipType));
        end;
    end;

    -- 上报首次弹窗
    ReportFstRun = function()
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kCloudGameListRpt;
        zTemp.str_action = "CloudList";
        zTemp.str_type   = "FirstShow";
        zTemp.str_json   = "{}";

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;

    -- 数据上报
    ReportClick = function(nType)
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zData["type"] = nType;

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kCloudGameListRpt;
        zTemp.str_action = "CloudList";
        zTemp.str_type   = "Click";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;

    -- 是否有效
    IsValidID = function(dwCtrlID)
        if (nil ~= dwCtrlID) and (0 ~= dwCtrlID) then
            return true;
        else
            return false;
        end;
    end;

    -- 窗口ID
    dwViewID = 0;
    sClkSink = nil;
    eTipType = 0;
    nEnabled = 0;

};